"""
File: department.py
Author: ershixiong
Date: 2023/12/12
describe:

"""
import requests
from jsonpath import jsonpath

from project202311_wework.wework.frame.apis.wework import Wework


class Department(Wework):
    # def __init__(self):
    #     self.access_token = self.get_access_token()

    def create(self, data):
        url_add = 'department/create'

        req = {
            'method': 'POST',
            'url': url_add,
            'json': data
        }
        r = self.send_api(req)
        return r

    def update(self, data):
        url_update = 'department/update'

        req = {
            'method': 'POST',
            'url': url_update,
            'json': data
        }
        r = self.send_api(req)
        return r

    def delete(self, depart_id):
        url_delete = 'department/delete'
        params = {
            'id': depart_id
        }
        req = {
            'method': 'POST',
            'url': url_delete,
            'params': params,
        }

        r = self.send_api(req)
        return r

    def list(self):
        url_list = 'department/simplelist'

        req = {
            'method': 'POST',
            'url': url_list,
        }
        r = self.send_api(req)
        return r

    def clear(self):
        r = self.list()
        depart_ids = jsonpath(r.json(), '$..id')
        for id in depart_ids:
            if id != 1:
                self.delete(id)

    def detail(self, depart_id):
        url_detail = 'department/get'
        params = {
            'id': depart_id,
        }
        req = {
            'method': 'GET',
            'url': url_detail,
            'params': params,
        }
        r = self.send_api(req)
        return r
